package ru.study.thinkin.chapter13;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

interface Factory<T> {
	 T create();
}


class Part  {
	static List <Factory<? extends Part>> partFactories = new ArrayList <Factory<? extends Part>>(); 
	private static Random rand = new Random();
		
	static {
		partFactories.add(new FuelFilter.Factory());
		partFactories.add(new AirFilter.Factory());
		partFactories.add(new FanBelt.Factory());
	}
	public static Part createRandom() {
		int n = rand.nextInt(partFactories.size());
		//System.out.println(n);
		
		return partFactories.get(n).create(); 
	}
	
	@Override
	public String toString() {	
		return getClass().getSimpleName();
	}	
}

class Filter extends Part{}

class FuelFilter extends Filter {
	public static class Factory implements ru.study.thinkin.chapter13.Factory<FuelFilter> {
		public FuelFilter create() {
			return new FuelFilter(); 
		}
	}
}

class AirFilter extends Filter {
	public static class Factory implements ru.study.thinkin.chapter13.Factory<AirFilter> {
		public AirFilter create() {
			return new AirFilter();
		}
	}
}

class Belt extends Part{}

class FanBelt extends Belt {
	public static class Factory implements ru.study.thinkin.chapter13.Factory<FanBelt> {

		@Override
		public FanBelt create() {
			return new FanBelt() ;
		}
	}	
}


public  class RegisteredFactories {

	public static void main(String[] args) {
		for(int i=0;i<10;i++) {
			System.out.println(Part.createRandom()); 
		}
		

	}

}
